#################################################################################
#
# Copyright (c) 2006 Michigan State University
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#################################################################################

#################################################################################
#
#	Author:
#		Nathan Collins <npcollins@gmail.com>
#
#################################################################################

"""
Misc utility functions.
"""

import md5
import sys
import types

def lists2dict(keylist, vallist):
	"""
	Take two lists, and use them to create a dict. Return None if fails.
	"""
	# the new dict
	newdict = {}
	
	try:
		# loop through lists
		for index in range(len(keylist)):
			newdict[keylist[index]] = vallist[index]

		# return the new dict
		return newdict

	except:
		return None
	

# function for generating a hash - MD5
def make_hash(unhashed):
	hashed = md5.new()
	hashed.update(unhashed)
	return hashed.hexdigest()
	
# check to see if the variable is a list
def isList(var): 
	t = type(var).__name__
	if t == 'list': 
		return 1
	else: 
		return 0

# verify that variable is a string
def isStr(var):
	if type(var) == StringType:
		return 1;
	else:
		return None

# write to stdout
def swrite(outstr):
	sys.stdout.write(outstr)
	sys.stdout.flush()

# progress indicator
class progress:
	"""
	Simple progress bar.
	"""
	def __init__(self, begin_msg = "Progress", done_msg = "Done", perdot = 100):
		self.out = sys.stdout
		self.begin_msg = begin_msg
		self.done_msg = done_msg
		self.perdot = perdot	# calls per dot
		self.curdot = 0 	# current calls
		self.out.flush()

	def begin(self):
		self.out.write(self.begin_msg + ' ')
		self.out.flush()

	def out(self, out_msg):
		"""
		Insert out_msg into the dot progress.
		"""
		self.out.write(out_msg)
		self.out.flush()

	def dot(self, dot_msg = '.'):
		self.curdot += 1
		if self.curdot % self.perdot == 0:
			self.curdot = 0
			self.out.write(dot_msg)
			self.out.flush()

	def done(self):
		self.out.write(' ' + self.done_msg + '\n')
		self.out.flush()

# eof
